home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: C syntax question
- Date: 11 Apr 1996 23:31:35 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4kktc7INN7cp@keats.ugrad.cs.ubc.ca>
- References: <4ki00k$a4@mailhub.scitec.com.au>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4ki00k$a4@mailhub.scitec.com.au>,
- John Saunders <johns@rd.scitec.com.au> wrote:
- >This is supposedly correct C, typedef names are ignored when defining
- >structure members. But not when defining variables so:
- > byte byte;
- >is an error when not inside a structure definition. Am I correct so far?
-
- Yes, but your rationale is off. What you are doing inside the struct
- declaration is redeclaring an identifier that was declared in an outer scope.
- It's sort of like declaring a local variable x in a function when an external x
- also exists.
-
- >I goal is to write a parser that handles the above correctly, all example
- >ANSI C parsers I have seen don't. I started playing around with mixing
- >typedefs and type keywords in the same declaration to see how C compilers
- >react. This is where it got strange. Consider the code.
- >
- > typedef unsigned char ubyte;
- > typedef signed char byte;
- >
- > ubyte signed i;
- > byte unsigned j;
- > signed ubyte k;
- > unsigned byte l;
-
- The above declarations are wrong and require that the compiler emit a
- diagnostic. You can't mix a typedef name with other specifiers like ``long'',
- ``float'', or ``unsigned''.
-
- >The compilers I tried allowed the declaration of i and j (some gave warnings
- >and others didn't). However none liked the declaration of k and l. From this
- >it would seem that a typedef name is allowed only as the first token in the
- >declaration. All the example ANSI C grammars that I have seen allow any number
-
- Careful: There is only *one* ANSI C grammar. It's defined in the standard.
-
- >of typedef names or type keywords in any order. This is causing a major
-
- You are missing the key concept that there are further constraints placed on
- what is an acceptable program. Not every string generated by the context free
- grammar is a valid C program. Some of the further constraints are semantic in
- nature (typechecking the use of an identifier against the declaration) but
- others augment the syntax in areas where placing the constraints into the
- grammar is not possible, or would complicate the grammar very badly.
-
- Read the standard about the precise contstraints placed on declarations. Some
- of the obvious ones are that you can't have two storage types in a specifier
- list: ``static auto int'' doesn't make sense, though the syntax allows it. The
- restriction that you can't stick extra specifiers like ``signed'' or
- ``unsigned'' on a typedef name, even though it is an integral type, is not
- quite as obvious.
-
- Check out exercise 4.10 in _Compilers Principles, Techniques and Tools_ by Aho,
- Sethi and Ullman: it discusses precisely the disadvantages of using the
- grammar to enforce certain constraints, such as disallowing conflicting or
- redundant declarations. One of those disadvantages is the explosive growth in
- the number of grammar symbols and parser states required to handle all the
- possibilities and reject the violations syntactically.
-
- >problem in being able to parse "byte byte;" correctly in the structure
- >definition. I.e. is the second occurance of "byte" supposed to be treated as
- >a typedef name or the member name?
-
- One way: You could parse the grammar as it is written, construct a tree and
- then traverse it looking for constraint violations, type checking etc. You
- could use an n-ary tree representation, in which your declaration-specifiers
- would turn into, say, a node having all the type specifiers as children. You
- wrould write code to ensure that there is ony one storage class, type
- qualifiers are not duplicated, only one type specifier is given (with the
- exception of ``signed'' and ``unsigned'' in conjunction with ``int'', ``char''
- or ``long''), etc.
-
- >Has anyone tackled this problem before?
-
- Anyone who has ever written a C compiler, I should think. :)
-
- Always keep in mind the distinction between syntax and semantics. The famous
- sentence that illustrates this is attributed to Chomsky, who was one of the
- first to pioneer the distinction between the two: ``Colourless green ideas
- sleep furiously''. Remember that and you are in good shape! :)
-
- In C, ideas can't be declared both colourless and green. The standard prohibits
- it.
-
-
- --
-
-